﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LiveUpdate
{
    public partial class frmLiveUpLoad : Form
    {
        public frmLiveUpLoad()
        {
            InitializeComponent();
        }

        private void btnScan_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                String filePathName = fileDialog.FileName;
                string fileName = fileDialog.SafeFileName;
                txtDir.Text = filePathName;
                P_Version mVersion = new P_Version();
                mVersion.GetModelByName(fileName);
                txtBZ.Text = mVersion.BZ;
                txtBBH.Text = mVersion.BBH.ToString();
                if (mVersion.SCRQ != null)
                    txtSCRQ.Text = mVersion.SCRQ.Value.ToShortDateString();
            }
        }

        private void frmLiveUpLoad_Load(object sender, EventArgs e)
        {
            DataSet dsVersion = P_Version.GetUpdateVersionList();
            gcVersion.DataSource = dsVersion.Tables[0];

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtDir.Text == "")
            {
                MessageBox.Show("请选中上传文件");
                txtDir.Focus();
                return;
            }

            if (txtBZ.Text == "")
            {
                MessageBox.Show("请输入上传功能名称");
                txtBZ.Focus();
                return;
            }

            P_SysPara mSysPara = new P_SysPara();
            mSysPara.GetModel(Utility.SystemName);
            FtpInfo _ftpInfo = new FtpInfo(mSysPara.FtpServer, "\\" + mSysPara.FtpSoftDir, mSysPara.FtpUser, mSysPara.FtpPwd, int.Parse(mSysPara.FtpPort));

            //FtpInfo _ftpInfo = new FtpInfo("127.0.0.1", "\\", "brs", "brs", 21);
            FTPClient _ftpClient = new FTPClient(_ftpInfo);

            try
            {
                _ftpClient.Connect();
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接FTP服务器失败！错误信息：" + ex.Message);
                return;
            }

            string sDescFile = System.IO.Path.GetFileName(txtDir.Text);
            try
            {
                _ftpClient.Rename(sDescFile, sDescFile + "." + DateTime.Now.ToString("yyyyMMddhhmmss"));
            }
            catch (Exception)
            {

            }

            this.Cursor = Cursors.WaitCursor;
            try
            {
                _ftpClient.Upload(sDescFile);
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Arrow;
                MessageBox.Show("文件上传出错,错误信息：" + ex.Message);
                return;
            }

            this.Cursor = Cursors.Arrow;
            int iBBH = 0;
            if (txtBBH.Text.Trim() == "")
            {
                iBBH = 1;
            }
            else
            {
                iBBH = int.Parse(txtBBH.Text.Trim()) + 1;
            }

            P_Version mVersion = new P_Version();
            mVersion.MC = sDescFile;
            mVersion.SCR = "";
            mVersion.SCRQ = DateTime.Now;
            mVersion.BZ = txtBZ.Text;
            mVersion.BBH = iBBH;
            mVersion.LJ = txtLJ.Text;
            try
            {
                mVersion.Add();
            }
            catch (Exception)
            {
                MessageBox.Show("上传日志未记载");
                return;
            }

            MessageBox.Show("文件上传成功");
            DataSet dsVersion = P_Version.GetUpdateVersionList();
            gcVersion.DataSource = dsVersion.Tables[0];
            txtDir.Clear();
            txtBZ.Clear();
            txtBBH.Clear();
            txtWJDX.Clear();
            txtLJ.SelectedIndex = -1;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvVersion.RowCount < 1) return;
            if (MessageBox.Show("是否要删除此记录,删除后此模块将不能自动更新", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            DataRow dr = gvVersion.GetDataRow(gvVersion.FocusedRowHandle);
            string sMC = dr["MC"].ToString();
            P_Version mVersion = new P_Version();
            mVersion.DeleteByName(sMC);

            DataSet dsVersion = P_Version.GetUpdateVersionList();
            gcVersion.DataSource = dsVersion.Tables[0];
        }
    }
}
